package io.lvdaxian.handwriting.set;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchTest {

    static CountDownLatch countDownLatch = new CountDownLatch(2);

    public static void main(String[] args) throws InterruptedException {

        new Thread(CountDownLatchTest::task01).start();
        new Thread(CountDownLatchTest::task02).start();

        new Thread(() -> {
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("这是任务03");
        }).start();

        countDownLatch.await();

        System.out.println("这是任务04");
    }

    static void task01() {
        try {
            Thread.sleep(1000);
            System.out.println("这是任务01");
            countDownLatch.countDown();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    static void task02() {
        try {
            Thread.sleep(2000);
            System.out.println("这是任务02");
            countDownLatch.countDown();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
